package vip.pwztx.api.controller.song;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import vip.pwztx.api.result.PageResult;
import vip.pwztx.api.service.song.SongService;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * 歌曲控制类
 *
 * @author zhoubaohua
 * @date 2019/10/10
 */
@RestController
@RequestMapping("/api/v1/song")
public class SongController {

    private final SongService songService;

    public SongController(SongService songService) {
        this.songService = songService;
    }

    /**
     * @param platform 歌曲平台
     * @param hash     hash值
     * @param type     歌曲音质类别
     * @return 歌曲信息
     */
    @GetMapping("/download/url/{platform}/{hash}/{type}")
    public String getSongDownloadUrl(@PathVariable String platform, @PathVariable String hash, @PathVariable String type, HttpServletRequest request) {
        String secret = request.getHeader("secret");
        if (StringUtils.isBlank(secret)) {
            return new JSONObject() {{
                put("code", 401);
                put("success", false);
                put("data", null);
                put("message", "未找到密钥！");
            }}.toJSONString();
        }
        String result = songService.getSongDownloadInfo(platform, hash, type, secret);
        if (StringUtils.isBlank(result)) {
            return new JSONObject() {{
                put("code", 404);
                put("success", false);
                put("data", null);
                put("message", "未获取到信息，请稍候再试！");
            }}.toJSONString();
        }
        return result;
    }

    @GetMapping("/search")
    public PageResult search(String keyword, String platform, Integer pageNo, Integer pageSize) {
        return songService.search(keyword, platform, pageNo, pageSize);
    }
}
